﻿using DevExpress.Utils;
using DevExpress.XtraEditors;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DevExpress.Base
{
    public partial class FormMessageBox : DevExpress.XtraEditors.XtraForm
    {
        public FormMessageBox()
        {
            InitializeComponent();
        }

        private void OnIconClick(object sender, EventArgs e)
        {
            var args = new XtraMessageBoxArgs
            {
                Caption = "警告",
                Text = "操作将导致数据丢失！",
                Buttons = new[] { DialogResult.OK, DialogResult.Cancel },
                ImageOptions = 
                {
                    // 为了方便使用了simpleButton1的图标
                    // 使用时，可以自己选择图标
                    SvgImage = this.sbIcon.ImageOptions.SvgImage,
                    //Image = this.simpleButton1.ImageOptions.Image,
                }
            };
            
            XtraMessageBox.Show(args);
        }

        private void OnButtonClick(object sender, EventArgs e)
        {
            var args = new XtraMessageBoxArgs
            {
                Caption = "提示",
                Text = "是否保存修改并退出？",
                Buttons = new[] { DialogResult.Yes, DialogResult.No, DialogResult.Cancel },
            };

            args.Showing += OnButtonArgsShowing;
            
            XtraMessageBox.Show(args);
        }

        private void OnButtonArgsShowing(object sender, XtraMessageShowingArgs e)
        {
            //备注: sicImages 为 SvgImageCollection
            foreach (var control in e.MessageBoxForm.Controls)
            {
                if (control is SimpleButton button)
                {
                    button.ImageOptions.SvgImageSize = new Size(16, 16);
                    switch (button.DialogResult)
                    {
                        case DialogResult.Yes:
                            // 对勾图标
                            button.ImageOptions.SvgImage = this.sicImages[1]; 
                            break;
                        case DialogResult.No:
                            // 叉号图标
                            button.ImageOptions.SvgImage = this.sicImages[3]; 
                            break;
                        case DialogResult.Cancel:
                            // 随便来个图标吧
                            button.ImageOptions.SvgImage = this.sicImages[0];
                            break;
                    }
                }
            }
        }

        private void OnFontClick(object sender, EventArgs e)
        {
            var args = new XtraMessageBoxArgs
            {
                Caption = "提示",
                Text = "我们要改变字体了？",
                Buttons = new[] { DialogResult.Yes, DialogResult.No },
            };

            args.Showing += OnFontArgsShowing;

            XtraMessageBox.Show(args);
        }

        private void OnFontArgsShowing(object sender, XtraMessageShowingArgs e)
        {
            // 正文字体大小
            e.Form.Appearance.Font = new Font("微软雅黑", 12, FontStyle.Bold);

            // 按钮字体与高度

            foreach (var control in e.MessageBoxForm.Controls)
            {
                if (control is SimpleButton button)
                {
                    button.Appearance.Font = new Font("黑体", 10);
                    button.Height = 30; // 增加按钮高度

                    button.ImageOptions.SvgImageSize = new Size(16, 16);
                    switch (button.DialogResult)
                    {
                        case DialogResult.Yes:
                            button.ImageOptions.SvgImage = this.sicImages[1];
                            button.Text = "是";
                            break;
                        case DialogResult.No:
                            button.ImageOptions.SvgImage = this.sicImages[3];
                            button.Text = "否";
                            break;

                    }
                }
            }
        }

        private void OnHtmlClick(object sender, EventArgs e)
        {
            var args = new XtraMessageBoxArgs
            {
                Caption = "提示",
                Buttons = new[] { DialogResult.Yes, DialogResult.No },
                AllowHtmlText = DefaultBoolean.True,
                Text = @"
                    <p align='center'>
                        <br><b>重要提示</b><br>
                        这次我们使用HTML自定义MessageBox。
                    </p>
                "
            };


            XtraMessageBox.Show(args);
        }

        private void OnLocalizeClick(object sender, EventArgs e)
        {
            var args = new XtraMessageBoxArgs
            {
                Caption = "提示",
                Text = "我们正在进行汉化演示？",
                Buttons = new[] { DialogResult.Yes, DialogResult.No, DialogResult.Cancel },
            };

            args.Showing += OnLocalizeArgsShowing;

            XtraMessageBox.Show(args);
        }

        private void OnLocalizeArgsShowing(object sender, XtraMessageShowingArgs e)
        {
            foreach (var control in e.MessageBoxForm.Controls)
            {
                if (control is SimpleButton button)
                {
                    switch (button.DialogResult)
                    {
                        case DialogResult.Yes:
                            button.Text = "是";
                            break;
                        case DialogResult.No:
                            button.Text = "否";
                            break;
                        case DialogResult.Cancel:
                            button.Text = "取消";
                            break;

                    }
                }
            }
        }
    }
}
